package ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;

import control.Operator;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;

public class AltBookUI extends JFrame {

	private JPanel contentPane;
	private JTextField booknameText;
	private JButton confirmButton;
	private JButton cancelButton;
	private JTextField altbooknameText;
	private JTextField altauthorText;
	private JTextField altpriceText;

	/**
	 * Create the frame.
	 */
	public AltBookUI() {
		setTitle("修改图书信息");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(450, 250, 480, 380);
		setLocationRelativeTo(null); // 居中显示

		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(20, 20, 20, 20));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 15));

		// 标题面板
		JLabel titleLabel = new JLabel("修改图书信息", SwingConstants.CENTER);
		titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
		titleLabel.setForeground(new Color(70, 130, 180));
		contentPane.add(titleLabel, BorderLayout.NORTH);

		// 表单面板
		JPanel formPanel = new JPanel();
		formPanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"),
				"图书修改信息", TitledBorder.LEADING, TitledBorder.TOP,
				new Font("微软雅黑", Font.PLAIN, 14), new Color(100, 100, 100)));
		contentPane.add(formPanel, BorderLayout.CENTER);
		formPanel.setLayout(null);

		// 原书名
		JLabel booknameLabel = new JLabel("原书名：");
		booknameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		booknameLabel.setBounds(60, 40, 80, 25);
		formPanel.add(booknameLabel);

		booknameText = new JTextField();
		booknameText.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		booknameText.setBounds(160, 40, 220, 30);
		formPanel.add(booknameText);
		booknameText.setColumns(10);

		// 修改后的书名
		JLabel altbooknameLabel = new JLabel("新书名：");
		altbooknameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		altbooknameLabel.setBounds(60, 90, 80, 25);
		formPanel.add(altbooknameLabel);

		altbooknameText = new JTextField();
		altbooknameText.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		altbooknameText.setColumns(10);
		altbooknameText.setBounds(160, 90, 220, 30);
		formPanel.add(altbooknameText);

		// 修改后的作者
		JLabel altauthorLabel = new JLabel("新作者：");
		altauthorLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		altauthorLabel.setBounds(60, 140, 80, 25);
		formPanel.add(altauthorLabel);

		altauthorText = new JTextField();
		altauthorText.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		altauthorText.setColumns(10);
		altauthorText.setBounds(160, 140, 220, 30);
		formPanel.add(altauthorText);

		// 修改后的价格
		JLabel altpriceLabel = new JLabel("新价格：");
		altpriceLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		altpriceLabel.setBounds(60, 190, 80, 25);
		formPanel.add(altpriceLabel);

		altpriceText = new JTextField();
		altpriceText.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		altpriceText.setColumns(10);
		altpriceText.setBounds(160, 190, 220, 30);
		formPanel.add(altpriceText);

		// 按钮面板
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 0));
		contentPane.add(buttonPanel, BorderLayout.SOUTH);

		confirmButton = new JButton("确定");
		confirmButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		confirmButton.setBackground(new Color(70, 130, 180));
		confirmButton.setForeground(Color.WHITE);
		confirmButton.setFocusPainted(false);
		confirmButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String bookname = booknameText.getText().trim();
				String altbookname = altbooknameText.getText().trim();
				String altauthor = altauthorText.getText().trim();
				float altprice = 0;

				// 验证输入
				if (bookname.isEmpty()) {
					JOptionPane.showMessageDialog(null, "原书名不能为空！", "输入错误", JOptionPane.WARNING_MESSAGE);
					return;
				}

				try {
					if (!altpriceText.getText().trim().isEmpty()) {
						altprice = Float.parseFloat(altpriceText.getText().trim());
						if (altprice <= 0) {
							JOptionPane.showMessageDialog(null, "价格必须大于0！", "输入错误", JOptionPane.WARNING_MESSAGE);
							return;
						}
					}

					Operator operator = new Operator();
					boolean isSuccess = operator.altBook(bookname, altbookname, altauthor, altprice);
					if (isSuccess) {
						JOptionPane.showMessageDialog(null, "修改成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
					} else {
						JOptionPane.showMessageDialog(null, "修改失败！", "失败", JOptionPane.ERROR_MESSAGE);
					}
				} catch (NumberFormatException e1) {
					JOptionPane.showMessageDialog(null, "请输入有效的价格数字！", "输入错误", JOptionPane.WARNING_MESSAGE);
				}
			}
		});
		buttonPanel.add(confirmButton);

		cancelButton = new JButton("取消");
		cancelButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		cancelButton.setBackground(new Color(220, 220, 220));
		cancelButton.setFocusPainted(false);
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AltBookUI.this.dispose();
			}
		});
		buttonPanel.add(cancelButton);
	}
}
